using System;
using UnityEngine;

namespace Weapons.Components.ComponentData
{
    [Serializable]
    public abstract class ComponentData
    {
        [SerializeField] [HideInInspector] private string name; //���Լ��ɣ������д���������ݽṹ�ĵ�һ�оͿ���Ϊ���ṹ������ı�ʶ����

        public ComponentData()
        {
            SetComponentName();
            SetComponentDependency();
        }
        //������Ϊ�˲��������ڼ��������ʾ����

        public Type ComponentDependecny { get; protected set; }

        public void SetComponentName()
        {
            name = GetType().Name;
        }

        protected abstract void SetComponentDependency(); //learn point ��Unity��virtual��abstract�Ǵ���һ�������

        public virtual void SetAttackDataNames()
        {
        }

        public virtual void InitializeAttackData(int numOfAttacks)
        {
        }
    }

    [Serializable]
    public abstract class ComponentData<T> : ComponentData
        where T : AttackData.AttackData //�̳�һ������������������ʵ�ֳ��󷽷������Խ�����ı�Ϊ������
    {
        [SerializeField] private T[] attackData;

        public T[] AttackData
        {
            get => attackData;
            private set => attackData = value;
        }

        public override void SetAttackDataNames()
        {
            base.SetAttackDataNames();

            for (var i = 0; i < AttackData.Length; i++) AttackData[i].SetAttackName(i + 1);
        }

        public override void InitializeAttackData(int numOfAttacks)
        {
            base.InitializeAttackData(numOfAttacks);

            var oldLen = attackData != null ? attackData.Length : 0; //�ǿռ��

            if (oldLen == numOfAttacks) return;


            Array.Resize(ref attackData, numOfAttacks); //����Ϸ�в�����ȥ�ı�һ������Ҫʹ�õ�����Ĵ�С������ķ���ʹ�ô����٣����Կ���
            if (oldLen < numOfAttacks)
                for (var i = oldLen; i < numOfAttacks; i++)
                {
                    var newObj = Activator.CreateInstance(typeof(T)) as T;
                    attackData[i] = newObj;
                }

            SetAttackDataNames();
        }
    }
}